home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 SRC / Mac / Modules / snd / Sndmodule.c < prev    next >
Text File  |  1996-04-12  |  23KB  |  857 lines

  1.  
  2. /* =========================== Module Snd =========================== */
  3.  
  4. #include "Python.h"
  5.  
  6.  
  7.  
  8. #define SystemSevenOrLater 1
  9.  
  10. #include "macglue.h"
  11. #include <Memory.h>
  12. #include <Dialogs.h>
  13. #include <Menus.h>
  14. #include <Controls.h>
  15.  
  16. extern PyObject *ResObj_New(Handle);
  17. extern int ResObj_Convert(PyObject *, Handle *);
  18. extern PyObject *OptResObj_New(Handle);
  19. extern int OptResObj_Convert(PyObject *, Handle *);
  20.  
  21. extern PyObject *WinObj_New(WindowPtr);
  22. extern int WinObj_Convert(PyObject *, WindowPtr *);
  23. extern PyTypeObject Window_Type;
  24. #define WinObj_Check(x) ((x)->ob_type == &Window_Type)
  25.  
  26. extern PyObject *DlgObj_New(DialogPtr);
  27. extern int DlgObj_Convert(PyObject *, DialogPtr *);
  28. extern PyTypeObject Dialog_Type;
  29. #define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
  30.  
  31. extern PyObject *MenuObj_New(MenuHandle);
  32. extern int MenuObj_Convert(PyObject *, MenuHandle *);
  33.  
  34. extern PyObject *CtlObj_New(ControlHandle);
  35. extern int CtlObj_Convert(PyObject *, ControlHandle *);
  36.  
  37. extern PyObject *GrafObj_New(GrafPtr);
  38. extern int GrafObj_Convert(PyObject *, GrafPtr *);
  39.  
  40. extern PyObject *BMObj_New(BitMapPtr);
  41. extern int BMObj_Convert(PyObject *, BitMapPtr *);
  42.  
  43. extern PyObject *PMObj_New(PixMapHandle);
  44. extern int PMObj_Convert(PyObject *, PixMapHandle *);
  45.  
  46. extern PyObject *WinObj_WhichWindow(WindowPtr);
  47.  
  48. #include <Sound.h>
  49.  
  50. #ifndef HAVE_UNIVERSAL_HEADERS
  51. #define SndCallBackUPP ProcPtr
  52. #define NewSndCallBackProc(x) ((SndCallBackProcPtr)(x))
  53. #define SndListHandle Handle
  54. #endif
  55.  
  56. #include <OSUtils.h> /* for Set(Current)A5 */
  57.  
  58. /* Create a SndCommand object (an (int, int, int) tuple) */
  59. static PyObject *
  60. SndCmd_New(SndCommand *pc)
  61. {
  62.     return Py_BuildValue("hhl", pc->cmd, pc->param1, pc->param2);
  63. }
  64.  
  65. /* Convert a SndCommand argument */
  66. static int
  67. SndCmd_Convert(PyObject *v, SndCommand *pc)
  68. {
  69.     int len;
  70.     pc->param1 = 0;
  71.     pc->param2 = 0;
  72.     if (PyTuple_Check(v)) {
  73.         if (PyArg_ParseTuple(v, "h|hl", &pc->cmd, &pc->param1, &pc->param2))
  74.             return 1;
  75.         PyErr_Clear();
  76.         return PyArg_ParseTuple(v, "hhs#", &pc->cmd, &pc->param1, &pc->param2, &len);
  77.     }
  78.     return PyArg_Parse(v, "h", &pc->cmd);
  79. }
  80.  
  81. /* Create a NumVersion object (a quintuple of integers) */
  82. static PyObject *
  83. NumVer_New(NumVersion nv)
  84. {
  85.     return Py_BuildValue("iiiii",
  86.                          nv.majorRev,
  87. #ifdef THINK_C
  88.                          nv.minorRev,
  89.                          nv.bugFixRev,
  90. #else
  91.                          (nv.minorAndBugRev>>4) & 0xf,
  92.                          nv.minorAndBugRev & 0xf,
  93. #endif
  94.                          nv.stage,
  95.                          nv.nonRelRev);
  96. }
  97.  
  98. static pascal void SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd); /* Forward */
  99.  
  100. static PyObject *Snd_Error;
  101.  
  102. /* --------------------- Object type SndChannel --------------------- */
  103.  
  104. staticforward PyTypeObject SndChannel_Type;
  105.  
  106. #define SndCh_Check(x) ((x)->ob_type == &SndChannel_Type)
  107.  
  108. typedef struct SndChannelObject {
  109.     PyObject_HEAD
  110.     SndChannelPtr ob_itself;
  111.     /* Members used to implement callbacks: */
  112.     PyObject *ob_callback;
  113.     long ob_A5;
  114.     SndCommand ob_cmd;
  115. } SndChannelObject;
  116.  
  117. static PyObject *SndCh_New(itself)
  118.     SndChannelPtr itself;
  119. {
  120.     SndChannelObject *it;
  121.     it = PyObject_NEW(SndChannelObject, &SndChannel_Type);
  122.     if (it == NULL) return NULL;
  123.     it->ob_itself = itself;
  124.     it->ob_callback = NULL;
  125.     it->ob_A5 = SetCurrentA5();
  126.     return (PyObject *)it;
  127. }
  128. static SndCh_Convert(v, p_itself)
  129.     PyObject *v;
  130.     SndChannelPtr *p_itself;
  131. {
  132.     if (!SndCh_Check(v))
  133.     {
  134.         PyErr_SetString(PyExc_TypeError, "SndChannel required");
  135.         return 0;
  136.     }
  137.     *p_itself = ((SndChannelObject *)v)->ob_itself;
  138.     return 1;
  139. }
  140.  
  141. static void SndCh_dealloc(self)
  142.     SndChannelObject *self;
  143. {
  144.     SndDisposeChannel(self->ob_itself, 1);
  145.     Py_XDECREF(self->ob_callback);
  146.     PyMem_DEL(self);
  147. }
  148.  
  149. static PyObject *SndCh_SndDoCommand(_self, _args)
  150.     SndChannelObject *_self;
  151.     PyObject *_args;
  152. {
  153.     PyObject *_res = NULL;
  154.     OSErr _err;
  155.     SndCommand cmd;
  156.     Boolean noWait;
  157.     if (!PyArg_ParseTuple(_args, "O&b",
  158.                           SndCmd_Convert, &cmd,
  159.                           &noWait))
  160.         return NULL;
  161.     _err = SndDoCommand(_self->ob_itself,
  162.                         &cmd,
  163.                         noWait);
  164.     if (_err != noErr) return PyMac_Error(_err);
  165.     Py_INCREF(Py_None);
  166.     _res = Py_None;
  167.     return _res;
  168. }
  169.  
  170. static PyObject *SndCh_SndDoImmediate(_self, _args)
  171.     SndChannelObject *_self;
  172.     PyObject *_args;
  173. {
  174.     PyObject *_res = NULL;
  175.     OSErr _err;
  176.     SndCommand cmd;
  177.     if (!PyArg_ParseTuple(_args, "O&",
  178.                           SndCmd_Convert, &cmd))
  179.         return NULL;
  180.     _err = SndDoImmediate(_self->ob_itself,
  181.                           &cmd);
  182.     if (_err != noErr) return PyMac_Error(_err);
  183.     Py_INCREF(Py_None);
  184.     _res = Py_None;
  185.     return _res;
  186. }
  187.  
  188. static PyObject *SndCh_SndPlay(_self, _args)
  189.     SndChannelObject *_self;
  190.     PyObject *_args;
  191. {
  192.     PyObject *_res = NULL;
  193.     OSErr _err;
  194.     SndListHandle sndHdl;
  195.     Boolean async;
  196.     if (!PyArg_ParseTuple(_args, "O&b",
  197.                           ResObj_Convert, &sndHdl,
  198.                           &async))
  199.         return NULL;
  200.     _err = SndPlay(_self->ob_itself,
  201.                    sndHdl,
  202.                    async);
  203.     if (_err != noErr) return PyMac_Error(_err);
  204.     Py_INCREF(Py_None);
  205.     _res = Py_None;
  206.     return _res;
  207. }
  208.  
  209. static PyObject *SndCh_SndStartFilePlay(_self, _args)
  210.     SndChannelObject *_self;
  211.     PyObject *_args;
  212. {
  213.     PyObject *_res = NULL;
  214.     OSErr _err;
  215.     short fRefNum;
  216.     short resNum;
  217.     long bufferSize;
  218.     Boolean async;
  219.     if (!PyArg_ParseTuple(_args, "hhlb",
  220.                           &fRefNum,
  221.                           &resNum,
  222.                           &bufferSize,
  223.                           &async))
  224.         return NULL;
  225.     _err = SndStartFilePlay(_self->ob_itself,
  226.                             fRefNum,
  227.                             resNum,
  228.                             bufferSize,
  229.                             0,
  230.                             0,
  231.                             0,
  232.                             async);
  233.     if (_err != noErr) return PyMac_Error(_err);
  234.     Py_INCREF(Py_None);
  235.     _res = Py_None;
  236.     return _res;
  237. }
  238.  
  239. static PyObject *SndCh_SndPauseFilePlay(_self, _args)
  240.     SndChannelObject *_self;
  241.     PyObject *_args;
  242. {
  243.     PyObject *_res = NULL;
  244.     OSErr _err;
  245.     if (!PyArg_ParseTuple(_args, ""))
  246.         return NULL;
  247.     _err = SndPauseFilePlay(_self->ob_itself);
  248.     if (_err != noErr) return PyMac_Error(_err);
  249.     Py_INCREF(Py_None);
  250.     _res = Py_None;
  251.     return _res;
  252. }
  253.  
  254. static PyObject *SndCh_SndStopFilePlay(_self, _args)
  255.     SndChannelObject *_self;
  256.     PyObject *_args;
  257. {
  258.     PyObject *_res = NULL;
  259.     OSErr _err;
  260.     Boolean quietNow;
  261.     if (!PyArg_ParseTuple(_args, "b",
  262.                           &quietNow))
  263.         return NULL;
  264.     _err = SndStopFilePlay(_self->ob_itself,
  265.                            quietNow);
  266.     if (_err != noErr) return PyMac_Error(_err);
  267.     Py_INCREF(Py_None);
  268.     _res = Py_None;
  269.     return _res;
  270. }
  271.  
  272. static PyObject *SndCh_SndChannelStatus(_self, _args)
  273.     SndChannelObject *_self;
  274.     PyObject *_args;
  275. {
  276.     PyObject *_res = NULL;
  277.     OSErr _err;
  278.     short theLength;
  279.     SCStatus theStatus__out__;
  280.     if (!PyArg_ParseTuple(_args, "h",
  281.                           &theLength))
  282.         return NULL;
  283.     _err = SndChannelStatus(_self->ob_itself,
  284.                             theLength,
  285.                             &theStatus__out__);
  286.     if (_err != noErr) return PyMac_Error(_err);
  287.     _res = Py_BuildValue("s#",
  288.                          (char *)&theStatus__out__, (int)sizeof(SCStatus));
  289.  theStatus__error__: ;
  290.     return _res;
  291. }
  292.  
  293. static PyMethodDef SndCh_methods[] = {
  294.     {"SndDoCommand", (PyCFunction)SndCh_SndDoCommand, 1,
  295.      "(SndCommand cmd, Boolean noWait) -> None"},
  296.     {"SndDoImmediate", (PyCFunction)SndCh_SndDoImmediate, 1,
  297.      "(SndCommand cmd) -> None"},
  298.     {"SndPlay", (PyCFunction)SndCh_SndPlay, 1,
  299.      "(SndListHandle sndHdl, Boolean async) -> None"},
  300.     {"SndStartFilePlay", (PyCFunction)SndCh_SndStartFilePlay, 1,
  301.      "(short fRefNum, short resNum, long bufferSize, Boolean async) -> None"},
  302.     {"SndPauseFilePlay", (PyCFunction)SndCh_SndPauseFilePlay, 1,
  303.      "() -> None"},
  304.     {"SndStopFilePlay", (PyCFunction)SndCh_SndStopFilePlay, 1,
  305.      "(Boolean quietNow) -> None"},
  306.     {"SndChannelStatus", (PyCFunction)SndCh_SndChannelStatus, 1,
  307.      "(short theLength) -> (SCStatus theStatus)"},
  308.     {NULL, NULL, 0}
  309. };
  310.  
  311. static PyMethodChain SndCh_chain = { SndCh_methods, NULL };
  312.  
  313. static PyObject *SndCh_getattr(self, name)
  314.     SndChannelObject *self;
  315.     char *name;
  316. {
  317.     return Py_FindMethodInChain(&SndCh_chain, (PyObject *)self, name);
  318. }
  319.  
  320. #define SndCh_setattr NULL
  321.  
  322. staticforward PyTypeObject SndChannel_Type = {
  323.     PyObject_HEAD_INIT(&PyType_Type)
  324.     0, /*ob_size*/
  325.     "SndChannel", /*tp_name*/
  326.     sizeof(SndChannelObject), /*tp_basicsize*/
  327.     0, /*tp_itemsize*/
  328.     /* methods */
  329.     (destructor) SndCh_dealloc, /*tp_dealloc*/
  330.     0, /*tp_print*/
  331.     (getattrfunc) SndCh_getattr, /*tp_getattr*/
  332.     (setattrfunc) SndCh_setattr, /*tp_setattr*/
  333. };
  334.  
  335. /* ------------------- End object type SndChannel ------------------- */
  336.  
  337.  
  338. static PyObject *Snd_SetSoundVol(_self, _args)
  339.     PyObject *_self;
  340.     PyObject *_args;
  341. {
  342.     PyObject *_res = NULL;
  343.     short level;
  344.     if (!PyArg_ParseTuple(_args, "h",
  345.                           &level))
  346.         return NULL;
  347.     SetSoundVol(level);
  348.     Py_INCREF(Py_None);
  349.     _res = Py_None;
  350.     return _res;
  351. }
  352.  
  353. static PyObject *Snd_GetSoundVol(_self, _args)
  354.     PyObject *_self;
  355.     PyObject *_args;
  356. {
  357.     PyObject *_res = NULL;
  358.     short level;
  359.     if (!PyArg_ParseTuple(_args, ""))
  360.         return NULL;
  361.     GetSoundVol(&level);
  362.     _res = Py_BuildValue("h",
  363.                          level);
  364.     return _res;
  365. }
  366.  
  367. static PyObject *Snd_SndNewChannel(_self, _args)
  368.     PyObject *_self;
  369.     PyObject *_args;
  370. {
  371.     PyObject *_res = NULL;
  372.     OSErr _err;
  373.     SndChannelPtr chan = 0;
  374.     short synth;
  375.     long init;
  376.     PyObject* userRoutine;
  377.     if (!PyArg_ParseTuple(_args, "hlO",
  378.                           &synth,
  379.                           &init,
  380.                           &userRoutine))
  381.         return NULL;
  382.     if (userRoutine != Py_None && !PyCallable_Check(userRoutine))
  383.     {
  384.         PyErr_SetString(PyExc_TypeError, "callback must be callable");
  385.         goto userRoutine__error__;
  386.     }
  387.     _err = SndNewChannel(&chan,
  388.                          synth,
  389.                          init,
  390.                          NewSndCallBackProc(SndCh_UserRoutine));
  391.     if (_err != noErr) return PyMac_Error(_err);
  392.     _res = Py_BuildValue("O&",
  393.                          SndCh_New, chan);
  394.     if (_res != NULL && userRoutine != Py_None)
  395.     {
  396.         SndChannelObject *p = (SndChannelObject *)_res;
  397.         p->ob_itself->userInfo = (long)p;
  398.         Py_INCREF(userRoutine);
  399.         p->ob_callback = userRoutine;
  400.     }
  401.  userRoutine__error__: ;
  402.     return _res;
  403. }
  404.  
  405. static PyObject *Snd_SndControl(_self, _args)
  406.     PyObject *_self;
  407.     PyObject *_args;
  408. {
  409.     PyObject *_res = NULL;
  410.     OSErr _err;
  411.     short id;
  412.     SndCommand cmd;
  413.     if (!PyArg_ParseTuple(_args, "h",
  414.                           &id))
  415.         return NULL;
  416.     _err = SndControl(id,
  417.                       &cmd);
  418.     if (_err != noErr) return PyMac_Error(_err);
  419.     _res = Py_BuildValue("O&",
  420.                          SndCmd_New, &cmd);
  421.     return _res;
  422. }
  423.  
  424. static PyObject *Snd_SndSoundManagerVersion(_self, _args)
  425.     PyObject *_self;
  426.     PyObject *_args;
  427. {
  428.     PyObject *_res = NULL;
  429.     long _rv;
  430.     if (!PyArg_ParseTuple(_args, ""))
  431.         return NULL;
  432.     _rv = SndSoundManagerVersion();
  433.     _res = Py_BuildValue("l",
  434.                          _rv);
  435.     return _res;
  436. }
  437.  
  438. static PyObject *Snd_SndManagerStatus(_self, _args)
  439.     PyObject *_self;
  440.     PyObject *_args;
  441. {
  442.     PyObject *_res = NULL;
  443.     OSErr _err;
  444.     short theLength;
  445.     SMStatus theStatus__out__;
  446.     if (!PyArg_ParseTuple(_args, "h",
  447.                           &theLength))
  448.         return NULL;
  449.     _err = SndManagerStatus(theLength,
  450.                             &theStatus__out__);
  451.     if (_err != noErr) return PyMac_Error(_err);
  452.     _res = Py_BuildValue("s#",
  453.                          (char *)&theStatus__out__, (int)sizeof(SMStatus));
  454.  theStatus__error__: ;
  455.     return _res;
  456. }
  457.  
  458. static PyObject *Snd_SndGetSysBeepState(_self, _args)
  459.     PyObject *_self;
  460.     PyObject *_args;
  461. {
  462.     PyObject *_res = NULL;
  463.     short sysBeepState;
  464.     if (!PyArg_ParseTuple(_args, ""))
  465.         return NULL;
  466.     SndGetSysBeepState(&sysBeepState);
  467.     _res = Py_BuildValue("h",
  468.                          sysBeepState);
  469.     return _res;
  470. }
  471.  
  472. static PyObject *Snd_SndSetSysBeepState(_self, _args)
  473.     PyObject *_self;
  474.     PyObject *_args;
  475. {
  476.     PyObject *_res = NULL;
  477.     OSErr _err;
  478.     short sysBeepState;
  479.     if (!PyArg_ParseTuple(_args, "h",
  480.                           &sysBeepState))
  481.         return NULL;
  482.     _err = SndSetSysBeepState(sysBeepState);
  483.     if (_err != noErr) return PyMac_Error(_err);
  484.     Py_INCREF(Py_None);
  485.     _res = Py_None;
  486.     return _res;
  487. }
  488.  
  489. static PyObject *Snd_MACEVersion(_self, _args)
  490.     PyObject *_self;
  491.     PyObject *_args;
  492. {
  493.     PyObject *_res = NULL;
  494.     long _rv;
  495.     if (!PyArg_ParseTuple(_args, ""))
  496.         return NULL;
  497.     _rv = MACEVersion();
  498.     _res = Py_BuildValue("l",
  499.                          _rv);
  500.     return _res;
  501. }
  502.  
  503. static PyObject *Snd_Comp3to1(_self, _args)
  504.     PyObject *_self;
  505.     PyObject *_args;
  506. {
  507.     PyObject *_res = NULL;
  508.     char *buffer__in__;
  509.     char *buffer__out__;
  510.     long buffer__len__;
  511.     int buffer__in_len__;
  512.     StateBlock *state__in__;
  513.     StateBlock state__out__;
  514.     int state__in_len__;
  515.     unsigned long numChannels;
  516.     unsigned long whichChannel;
  517.     if (!PyArg_ParseTuple(_args, "s#s#ll",
  518.                           &buffer__in__, &buffer__in_len__,
  519.                           (char **)&state__in__, &state__in_len__,
  520.                           &numChannels,
  521.                           &whichChannel))
  522.         return NULL;
  523.     if ((buffer__out__ = malloc(buffer__in_len__)) == NULL)
  524.     {
  525.         PyErr_NoMemory();
  526.         goto buffer__error__;
  527.     }
  528.     buffer__len__ = buffer__in_len__;
  529.     if (state__in_len__ != sizeof(StateBlock))
  530.     {
  531.         PyErr_SetString(PyExc_TypeError, "buffer length should be sizeof(StateBlock)");
  532.         goto state__error__;
  533.     }
  534.     Comp3to1(buffer__in__, buffer__out__, (long)buffer__len__,
  535.              state__in__, &state__out__,
  536.              numChannels,
  537.              whichChannel);
  538.     _res = Py_BuildValue("s#s#",
  539.                          buffer__out__, (int)buffer__len__,
  540.                          (char *)&state__out__, (int)sizeof(StateBlock));
  541.  state__error__: ;
  542.     free(buffer__out__);
  543.  buffer__error__: ;
  544.     return _res;
  545. }
  546.  
  547. static PyObject *Snd_Exp1to3(_self, _args)
  548.     PyObject *_self;
  549.     PyObject *_args;
  550. {
  551.     PyObject *_res = NULL;
  552.     char *buffer__in__;
  553.     char *buffer__out__;
  554.     long buffer__len__;
  555.     int buffer__in_len__;
  556.     StateBlock *state__in__;
  557.     StateBlock state__out__;
  558.     int state__in_len__;
  559.     unsigned long numChannels;
  560.     unsigned long whichChannel;
  561.     if (!PyArg_ParseTuple(_args, "s#s#ll",
  562.                           &buffer__in__, &buffer__in_len__,
  563.                           (char **)&state__in__, &state__in_len__,
  564.                           &numChannels,
  565.                           &whichChannel))
  566.         return NULL;
  567.     if ((buffer__out__ = malloc(buffer__in_len__)) == NULL)
  568.     {
  569.         PyErr_NoMemory();
  570.         goto buffer__error__;
  571.     }
  572.     buffer__len__ = buffer__in_len__;
  573.     if (state__in_len__ != sizeof(StateBlock))
  574.     {
  575.         PyErr_SetString(PyExc_TypeError, "buffer length should be sizeof(StateBlock)");
  576.         goto state__error__;
  577.     }
  578.     Exp1to3(buffer__in__, buffer__out__, (long)buffer__len__,
  579.             state__in__, &state__out__,
  580.             numChannels,
  581.             whichChannel);
  582.     _res = Py_BuildValue("s#s#",
  583.                          buffer__out__, (int)buffer__len__,
  584.                          (char *)&state__out__, (int)sizeof(StateBlock));
  585.  state__error__: ;
  586.     free(buffer__out__);
  587.  buffer__error__: ;
  588.     return _res;
  589. }
  590.  
  591. static PyObject *Snd_Comp6to1(_self, _args)
  592.     PyObject *_self;
  593.     PyObject *_args;
  594. {
  595.     PyObject *_res = NULL;
  596.     char *buffer__in__;
  597.     char *buffer__out__;
  598.     long buffer__len__;
  599.     int buffer__in_len__;
  600.     StateBlock *state__in__;
  601.     StateBlock state__out__;
  602.     int state__in_len__;
  603.     unsigned long numChannels;
  604.     unsigned long whichChannel;
  605.     if (!PyArg_ParseTuple(_args, "s#s#ll",
  606.                           &buffer__in__, &buffer__in_len__,
  607.                           (char **)&state__in__, &state__in_len__,
  608.                           &numChannels,
  609.                           &whichChannel))
  610.         return NULL;
  611.     if ((buffer__out__ = malloc(buffer__in_len__)) == NULL)
  612.     {
  613.         PyErr_NoMemory();
  614.         goto buffer__error__;
  615.     }
  616.     buffer__len__ = buffer__in_len__;
  617.     if (state__in_len__ != sizeof(StateBlock))
  618.     {
  619.         PyErr_SetString(PyExc_TypeError, "buffer length should be sizeof(StateBlock)");
  620.         goto state__error__;
  621.     }
  622.     Comp6to1(buffer__in__, buffer__out__, (long)buffer__len__,
  623.              state__in__, &state__out__,
  624.              numChannels,
  625.              whichChannel);
  626.     _res = Py_BuildValue("s#s#",
  627.                          buffer__out__, (int)buffer__len__,
  628.                          (char *)&state__out__, (int)sizeof(StateBlock));
  629.  state__error__: ;
  630.     free(buffer__out__);
  631.  buffer__error__: ;
  632.     return _res;
  633. }
  634.  
  635. static PyObject *Snd_Exp1to6(_self, _args)
  636.     PyObject *_self;
  637.     PyObject *_args;
  638. {
  639.     PyObject *_res = NULL;
  640.     char *buffer__in__;
  641.     char *buffer__out__;
  642.     long buffer__len__;
  643.     int buffer__in_len__;
  644.     StateBlock *state__in__;
  645.     StateBlock state__out__;
  646.     int state__in_len__;
  647.     unsigned long numChannels;
  648.     unsigned long whichChannel;
  649.     if (!PyArg_ParseTuple(_args, "s#s#ll",
  650.                           &buffer__in__, &buffer__in_len__,
  651.                           (char **)&state__in__, &state__in_len__,
  652.                           &numChannels,
  653.                           &whichChannel))
  654.         return NULL;
  655.     if ((buffer__out__ = malloc(buffer__in_len__)) == NULL)
  656.     {
  657.         PyErr_NoMemory();
  658.         goto buffer__error__;
  659.     }
  660.     buffer__len__ = buffer__in_len__;
  661.     if (state__in_len__ != sizeof(StateBlock))
  662.     {
  663.         PyErr_SetString(PyExc_TypeError, "buffer length should be sizeof(StateBlock)");
  664.         goto state__error__;
  665.     }
  666.     Exp1to6(buffer__in__, buffer__out__, (long)buffer__len__,
  667.             state__in__, &state__out__,
  668.             numChannels,
  669.             whichChannel);
  670.     _res = Py_BuildValue("s#s#",
  671.                          buffer__out__, (int)buffer__len__,
  672.                          (char *)&state__out__, (int)sizeof(StateBlock));
  673.  state__error__: ;
  674.     free(buffer__out__);
  675.  buffer__error__: ;
  676.     return _res;
  677. }
  678.  
  679. static PyObject *Snd_GetSysBeepVolume(_self, _args)
  680.     PyObject *_self;
  681.     PyObject *_args;
  682. {
  683.     PyObject *_res = NULL;
  684.     OSErr _err;
  685.     long level;
  686.     if (!PyArg_ParseTuple(_args, ""))
  687.         return NULL;
  688.     _err = GetSysBeepVolume(&level);
  689.     if (_err != noErr) return PyMac_Error(_err);
  690.     _res = Py_BuildValue("l",
  691.                          level);
  692.     return _res;
  693. }
  694.  
  695. static PyObject *Snd_SetSysBeepVolume(_self, _args)
  696.     PyObject *_self;
  697.     PyObject *_args;
  698. {
  699.     PyObject *_res = NULL;
  700.     OSErr _err;
  701.     long level;
  702.     if (!PyArg_ParseTuple(_args, "l",
  703.                           &level))
  704.         return NULL;
  705.     _err = SetSysBeepVolume(level);
  706.     if (_err != noErr) return PyMac_Error(_err);
  707.     Py_INCREF(Py_None);
  708.     _res = Py_None;
  709.     return _res;
  710. }
  711.  
  712. static PyObject *Snd_GetDefaultOutputVolume(_self, _args)
  713.     PyObject *_self;
  714.     PyObject *_args;
  715. {
  716.     PyObject *_res = NULL;
  717.     OSErr _err;
  718.     long level;
  719.     if (!PyArg_ParseTuple(_args, ""))
  720.         return NULL;
  721.     _err = GetDefaultOutputVolume(&level);
  722.     if (_err != noErr) return PyMac_Error(_err);
  723.     _res = Py_BuildValue("l",
  724.                          level);
  725.     return _res;
  726. }
  727.  
  728. static PyObject *Snd_SetDefaultOutputVolume(_self, _args)
  729.     PyObject *_self;
  730.     PyObject *_args;
  731. {
  732.     PyObject *_res = NULL;
  733.     OSErr _err;
  734.     long level;
  735.     if (!PyArg_ParseTuple(_args, "l",
  736.                           &level))
  737.         return NULL;
  738.     _err = SetDefaultOutputVolume(level);
  739.     if (_err != noErr) return PyMac_Error(_err);
  740.     Py_INCREF(Py_None);
  741.     _res = Py_None;
  742.     return _res;
  743. }
  744.  
  745. static PyObject *Snd_GetSoundHeaderOffset(_self, _args)
  746.     PyObject *_self;
  747.     PyObject *_args;
  748. {
  749.     PyObject *_res = NULL;
  750.     OSErr _err;
  751.     SndListHandle sndHandle;
  752.     long offset;
  753.     if (!PyArg_ParseTuple(_args, "O&",
  754.                           ResObj_Convert, &sndHandle))
  755.         return NULL;
  756.     _err = GetSoundHeaderOffset(sndHandle,
  757.                                 &offset);
  758.     if (_err != noErr) return PyMac_Error(_err);
  759.     _res = Py_BuildValue("l",
  760.                          offset);
  761.     return _res;
  762. }
  763.  
  764. static PyMethodDef Snd_methods[] = {
  765.     {"SetSoundVol", (PyCFunction)Snd_SetSoundVol, 1,
  766.      "(short level) -> None"},
  767.     {"GetSoundVol", (PyCFunction)Snd_GetSoundVol, 1,
  768.      "() -> (short level)"},
  769.     {"SndNewChannel", (PyCFunction)Snd_SndNewChannel, 1,
  770.      "(short synth, long init, PyObject* userRoutine) -> (SndChannelPtr chan)"},
  771.     {"SndControl", (PyCFunction)Snd_SndControl, 1,
  772.      "(short id) -> (SndCommand cmd)"},
  773.     {"SndSoundManagerVersion", (PyCFunction)Snd_SndSoundManagerVersion, 1,
  774.      "() -> (long _rv)"},
  775.     {"SndManagerStatus", (PyCFunction)Snd_SndManagerStatus, 1,
  776.      "(short theLength) -> (SMStatus theStatus)"},
  777.     {"SndGetSysBeepState", (PyCFunction)Snd_SndGetSysBeepState, 1,
  778.      "() -> (short sysBeepState)"},
  779.     {"SndSetSysBeepState", (PyCFunction)Snd_SndSetSysBeepState, 1,
  780.      "(short sysBeepState) -> None"},
  781.     {"MACEVersion", (PyCFunction)Snd_MACEVersion, 1,
  782.      "() -> (long _rv)"},
  783.     {"Comp3to1", (PyCFunction)Snd_Comp3to1, 1,
  784.      "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
  785.     {"Exp1to3", (PyCFunction)Snd_Exp1to3, 1,
  786.      "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
  787.     {"Comp6to1", (PyCFunction)Snd_Comp6to1, 1,
  788.      "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
  789.     {"Exp1to6", (PyCFunction)Snd_Exp1to6, 1,
  790.      "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
  791.     {"GetSysBeepVolume", (PyCFunction)Snd_GetSysBeepVolume, 1,
  792.      "() -> (long level)"},
  793.     {"SetSysBeepVolume", (PyCFunction)Snd_SetSysBeepVolume, 1,
  794.      "(long level) -> None"},
  795.     {"GetDefaultOutputVolume", (PyCFunction)Snd_GetDefaultOutputVolume, 1,
  796.      "() -> (long level)"},
  797.     {"SetDefaultOutputVolume", (PyCFunction)Snd_SetDefaultOutputVolume, 1,
  798.      "(long level) -> None"},
  799.     {"GetSoundHeaderOffset", (PyCFunction)Snd_GetSoundHeaderOffset, 1,
  800.      "(SndListHandle sndHandle) -> (long offset)"},
  801.     {NULL, NULL, 0}
  802. };
  803.  
  804.  
  805.  
  806. /* Routine passed to Py_AddPendingCall -- call the Python callback */
  807. static int
  808. SndCh_CallCallBack(arg)
  809.     void *arg;
  810. {
  811.     SndChannelObject *p = (SndChannelObject *)arg;
  812.     PyObject *args;
  813.     PyObject *res;
  814.     args = Py_BuildValue("(O(hhl))",
  815.                          p, p->ob_cmd.cmd, p->ob_cmd.param1, p->ob_cmd.param2);
  816.     res = PyEval_CallObject(p->ob_callback, args);
  817.     Py_DECREF(args);
  818.     if (res == NULL)
  819.         return -1;
  820.     Py_DECREF(res);
  821.     return 0;
  822. }
  823.  
  824. /* Routine passed to NewSndChannel -- schedule a call to SndCh_CallCallBack */
  825. static pascal void
  826. SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd)
  827. {
  828.     SndChannelObject *p = (SndChannelObject *)(chan->userInfo);
  829.     if (p->ob_callback != NULL) {
  830.         long A5 = SetA5(p->ob_A5);
  831.         p->ob_cmd = *cmd;
  832.         Py_AddPendingCall(SndCh_CallCallBack, (void *)p);
  833.         SetA5(A5);
  834.     }
  835. }
  836.  
  837.  
  838. void initSnd()
  839. {
  840.     PyObject *m;
  841.     PyObject *d;
  842.  
  843.  
  844.  
  845.  
  846.  
  847.     m = Py_InitModule("Snd", Snd_methods);
  848.     d = PyModule_GetDict(m);
  849.     Snd_Error = PyMac_GetOSErrException();
  850.     if (Snd_Error == NULL ||
  851.         PyDict_SetItemString(d, "Error", Snd_Error) != 0)
  852.         Py_FatalError("can't initialize Snd.Error");
  853. }
  854.  
  855. /* ========================= End module Snd ========================= */
  856.  
  857.